package com.qinhailin.portal.dbm.service;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.jfinal.aop.Inject;
import com.qinhailin.portal.dbm.po.Message;


public class TimedService {
	
	private static final Logger log = Logger.getLogger(TimedService.class);
	
	public static final ScheduledExecutorService timedService = Executors.newScheduledThreadPool(1);
	
	@Inject
	private DPListNoticeServiceImpl listnoticeService;
	@Inject
	private WXPusherServiceImpl wxPusherService;
	//DBM dbm = DBM.getInstance();
	
	/**
	 * <p>定时任务</p>
	 */
	public final Runnable notice = new Runnable(){

		@Override
		public void run() {
			List<Message> msgs = listnoticeService.dp_listnotice();
			if(msgs != null) {
				for(Message m : msgs) {
					wxPusherService.send(m);
					//System.out.println("预警通知:" + m.getXMsg() + ",uid:" + m.getXWXOpenID());
					log.info("预警通知:" + m.getXMsg() + ",uid:" + m.getXWXOpenID());
				}
			}
		}
	};
	
	private TimedService() {
		timedService.scheduleWithFixedDelay(notice, 1000, 6000, TimeUnit.MILLISECONDS);
		//timedService.scheduleAtFixedRate(task, 10, 20, TimeUnit.SECONDS);
		
		// 定时将temp的数据转移到正式表中去
	}

	private static TimedService instance = null;
	// 单例模式
	public synchronized static TimedService getInstance() {
		if(instance == null) {
			instance = new TimedService();
		}
		return instance;
	}
}
